/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;
import client.ClientThread;
import view.ServerView;
import java.net.*;

class ServerThread implements Runnable {
    private Socket clientSocket;
    private int id;
    private Server server;

    private boolean clientClosed = false;

    public ServerThread(Socket clientSocket, int id, Server server) {
        this.clientSocket = clientSocket;
        this.id = id;
        ServerView.printMessage("Connection " + id + " established with: " + clientSocket + "\n");

        try {
            clientSocket.setSoTimeout(10000);
        } catch (Throwable e) {
            ServerView.printMessage("Error: Client timed out");
            clientClosed =true;
        }
    }

    public void run() {

        try {
            while(clientClosed == false) {
               if(ClientThread.checkConnectionClosed() == true) {
                    clientClosed = true;
               }
            }
            ServerView.printMessage( "Connection " + id + " closed." );
            clientSocket.close();
        } catch (Throwable e) {
            ServerView.printMessage("Error: Connection could not be closed.");
        }
    }
}
